/*
 * Copyright (C) 2011 JiangHongTiao <jjurco.sk_gmail.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package sk.lieskove.jianghongtiao.websearch.document.preprocessing.actions;

import org.apache.log4j.Logger;
import sk.lieskove.jianghongtiao.common.utils.FileUtils;
import sk.lieskove.jianghongtiao.common.utils.PropertiesUtils;
import sk.lieskove.jianghongtiao.websearch.persistence.WebSearchResponse;

import java.io.*;
import java.util.Scanner;

/**
 * Date of create: Oct 11, 2011
 *
 * @author JiangHongTiao <jjurco.sk_gmail.com>
 * @version 2011.1011
 */
public class Enc2Utf8 implements PreprocessAction {

    private Logger log = Logger.getLogger(Enc2Utf8.class);
    private PropertiesUtils pu = new PropertiesUtils(Enc2Utf8.class);

    @Override
    public WebSearchResponse preprocess(WebSearchResponse response) {
        if(response.getFile() == null){
            return response;
        }
        if((response.getEncoding() != null) && 
                (response.getEncoding().toLowerCase().equals("void"))){
            return response;
        }
        Scanner scanner = null;
        Writer out = null;
        File outFile = FileUtils.getTempFile("Enc2Utf8_", "");
        try {
            if(response.getEncoding() != null){
                scanner = new Scanner(new FileInputStream(response.getFile()),
                    response.getEncoding());
            } else {
                scanner = new Scanner(new FileInputStream(response.getFile()));
            }
            
            out = new OutputStreamWriter(new FileOutputStream(outFile), "UTF-8");
            while (scanner.hasNextLine()) {
                out.write(scanner.nextLine());
                out.write(System.getProperty("line.separator"));
            }
            response.setFile(outFile);
        } catch (UnsupportedEncodingException ex) {
            log.error("This encoding is not supported: "+response.getEncoding());
        } catch (FileNotFoundException ex) {
            log.error("File was not found!");
        } catch (IOException ex) {
            log.error("I/O exception was thrown when trying to convert file:"+
                    response.getFile().getAbsolutePath() + " to UTF-8 encoding"
                    + " into file: "+outFile.getAbsolutePath());
        } finally {
            try {
                if(scanner != null){
                    scanner.close();
                }
                if(out != null){
                    out.close();
                }
            } catch (IOException ex) {
                log.error("Cannot close scanner or output stream!");
            }
        }
        return response;
    }
}
